package org.h.apiinterface.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.h.apiinterface.mapper.UserMapper;
import org.h.apiinterface.model.dto.user.RegisterdDTO;
import org.h.apiinterface.model.dto.user.UserLoginDTO;
import org.h.apiinterface.model.dto.user.UserQueryDTO;
import org.h.apiinterface.model.entity.User;
import org.h.apiinterface.model.reslut.ResultPage;
import org.h.apiinterface.service.UserService;
import org.h.apiinterface.util.BaseException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
* @author haoshuai
* @description 针对表【user(用户)】的数据库操作Service实现
* @createDate 2024-04-26 19:36:23
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService {

    @Override
    public User login(UserLoginDTO userLoginDTO) {
        String userPassword = userLoginDTO.getUserPassword();
        String userAccount = userLoginDTO.getUserAccount();
        User user = this.getOne(new LambdaQueryWrapper<User>().eq(User::getUserAccount,userAccount));
        String userPassword1 = user.getUserPassword();
        boolean res = userPassword.equals(userPassword1);
        if (res){
            return user;
        }else {
            throw new BaseException("密码不正确");
        }

    }

    @Override
    public ResultPage getListPage(UserQueryDTO queryPageDTO) {
        Page<User> page = new Page<>(queryPageDTO.getCurrent(), queryPageDTO.getPageSize());
        IPage<User> userIPage = baseMapper.selectPage(page,new QueryWrapper<>());
        ResultPage resultPage = new ResultPage();
        resultPage.setRecords(userIPage.getRecords());
        resultPage.setCurrent(userIPage.getCurrent());
        resultPage.setPages(userIPage.getPages());
        resultPage.setTotal(userIPage.getTotal());
        return resultPage;
    }

    @Override
    public User register(RegisterdDTO registerdDTO) {
        String userAccount = registerdDTO.getUserAccount();
        User user = baseMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUserAccount, userAccount));
        if (user == null){
            throw new BaseException("该用户名已经重复,请重新设置");
        }
        if (!registerdDTO.getUserPassword().equals(registerdDTO.getCheckPassword())) {
            throw new BaseException("俩次输入的密码不一致");
        }
        user = new User();
        BeanUtils.copyProperties(registerdDTO,user);
        baseMapper.insert(user);
        return null;
    }

    @Override
    public Boolean isPermission(String accessKey, String secretKey) {
        // todo 添加一个不拦截的accessKey secretKey
        if ("123".equals(accessKey) && "123".equals(secretKey)){
            return true;
        }
        User user = baseMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getAccessKey, accessKey).eq(User::getSecretKey, secretKey));
        return user != null;
    }
}




